package com.example.ui.leftmenu;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.MyMoveView;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.service.MainService;
import com.example.ui.MainActivity;
import com.example.ui.contact.MsgState;
import com.example.ui.contact.chat.PushMessageReceiver;
import com.example.ui.homepage.MyInformationActivity;
import com.example.util.AsyncLoadImage;
import com.example.util.Util;

public class LeftMenuView {
	
	private static MainActivity mainActivity;
	private View view;
	private MyMoveView moveView;
	private LinearLayout titleLayout;
	private int currentTab=0;
	public MyListAdapter adapter;  
	public ExpandableListView mExpandableListView;
	public static ImageView titleImg;
	public static TextView titleName;
	public static TextView shareNum,praiseNum,collectNum;
	private AsyncLoadImage asyncLoadImage;
	
	private String[] groups = {"爱学习","乐分享","其他"};//,"联系"
	private String[][] childText = new String[][]{{"选课中心","我的收藏","线下课程活动"},
			{"华商访谈"},//"最新分享","圈子"},
			//{"消息","我的学友"},
			{"下载管理","账号设置"}};
	private int[][] childImg = {{R.drawable.item01_img,R.drawable.item02_img,R.drawable.item03_img},
			{R.drawable.item04_img},//R.drawable.item05_img,R.drawable.item06_img},
			//{R.drawable.item07_img,R.drawable.item08_img},
			{R.drawable.item10_img,R.drawable.item09_img}};
	private String myId;
	public  String headPath;
	public static int msgNum = 0;
	public static int canScroll=0;
	
	
	/**
	 * 构造初始化
	 * @param mainActivity
	 * @param moveView
	 */
	@SuppressWarnings("static-access")
	public LeftMenuView(MainActivity mainActivity,MyMoveView moveView){
		this.mainActivity = mainActivity;
		this.moveView = moveView;
		this.view = LayoutInflater.from(mainActivity).inflate(R.layout.leftmenu_listview, null);
		myId=HSApplication.sp.getString("userid","").toString();
		headPath=HSApplication.sp.getString("imgHead","").toString();
		createView();
		setViewProperty();
	}
	public  Handler headPic =new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				String headurl=HSApplication.sp.getString("imgHead","").toString();
				asyncLoadImage.imageLoader.displayImage(headurl, titleImg,asyncLoadImage.options);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	public void createView() {
		titleImg=(ImageView)view.findViewById(R.id.left_title_img);
		titleName=(TextView)view.findViewById(R.id.left_title_img_name);
		mExpandableListView = (ExpandableListView)view.findViewById(R.id.expandableListView1);
		titleLayout = (LinearLayout)view.findViewById(R.id.leftmenu_titleLayout);
		shareNum=(TextView)view.findViewById(R.id.left_title_share_num);
		praiseNum=(TextView)view.findViewById(R.id.left_title_praise_num);
		collectNum=(TextView)view.findViewById(R.id.left_title_collect_num);
		String name= HSApplication.sp.getString("username","").toString();
		titleName.setText(name);
		String headurl=HSApplication.sp.getString("imgHead","").toString();
		asyncLoadImage=new AsyncLoadImage();
		asyncLoadImage.imageLoader.displayImage(headurl, titleImg,asyncLoadImage.options);
		headPic.sendEmptyMessage(1);
		//获取该用户的分享、赞、收藏的数量
		try {
			Communication.getStatisticsNumber(HSApplication.sp.getString("userid","").toString());
		} catch (Exception e) {
			Util.toast(mainActivity, "网络连接异常");
			e.printStackTrace();
		}
	}
	public void setViewProperty() {
		titleImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startHomePage();
				
			}
		});
		adapter = new MyListAdapter(view);
		mExpandableListView.setAdapter(adapter);
		//遍历所有group,将所有项设置成默认展开
		int groupCount = mExpandableListView.getCount();
		for (int i=0; i<groupCount; i++) {
			mExpandableListView.expandGroup(i);
		};
		
		//组被点击事件
		mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return true;//返回true，设置组不能被点击，可能2.1之前的不支持
			}
		});
		//子项点击事件
		mExpandableListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				startItem(groupPosition,childPosition);
				//v.invalidate();
				return true;
			}
		});
		
	}
	
	/**
	 * 进入主界面
	 */
	public void startHomePage(){
		if(!startInit(1)){
			setCurrentTab(1);
			if(null==mainActivity.item01View.view){
				mainActivity.item01View.init();
				mainActivity.item01View.setMyMoveView(moveView);
			}
			moveView.setMainView(mainActivity.item01View, null, 1);
		}
		Intent intent = new Intent(mainActivity, MyInformationActivity.class);
		intent.putExtra("headPath", headPath);
		intent.putExtra("myId", myId);
		mainActivity.startActivity(intent);
	}
	
	/**
	 * 从左边的listview跳转到每一个相应的页面
	 * @param tabId 对应的listview.Item
	 */
	private void startItem(int groupPosition, int childPosition) {
		if(childText[groupPosition][childPosition].toString().equals("选课中心")){
			if(!startInit(1)){
				//打开视图
				setCurrentTab(1);
				if(mainActivity.item01View.view==null){
					canScroll=1;
					mainActivity.item01View.init();
					moveView.setMainView(mainActivity.item01View, null,1);
					mainActivity.item01View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item01View, null,1);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("我的收藏")){
			if(!startInit(2)){
				//打开视图
				setCurrentTab(2);
				if(mainActivity.item02View.view==null){
					mainActivity.item02View.init();
					mainActivity.item02View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item02View, null,2);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("线下课程活动")){
			if(!startInit(3)){
				//打开视图
				setCurrentTab(3);
				
				if(mainActivity.item03View.view==null){
					mainActivity.item03View.init();
					mainActivity.item03View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item03View, null,3);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("华商访谈")){
			if(!startInit(4)){
				//打开视图
				setCurrentTab(4);
				if(mainActivity.item04View.view==null){
					mainActivity.item04View.init();
					mainActivity.item04View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item04View, null,4);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("最新分享")){
			if(!startInit(5)){
				//打开视图
				setCurrentTab(5);
				if(mainActivity.item05View.view==null){
					mainActivity.item05View.init();
					mainActivity.item05View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item05View, null,5);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("圈子")){
//			mainActivity.startActivity(new Intent(mainActivity, CircleGroupActivity.class));
			if(!startInit(6)){
				//打开视图
				setCurrentTab(6);
				if(mainActivity.item06View.view==null){
					mainActivity.item06View.init();
					mainActivity.item06View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item06View, null,6);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("消息")){
			if(!startInit(7)){
				//打开视图
				setCurrentTab(7);
				if(mainActivity.item07View.view==null){
					mainActivity.item07View.init();
					mainActivity.item07View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item07View, null,7);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("我的学友")){
			if(!startInit(8)){
				//打开视图
				MsgState.state = MsgState.FD_LIST_MSG_STATE;
				setCurrentTab(8);
				if(mainActivity.item08View.view==null){
					mainActivity.item08View.init();
					mainActivity.item08View.setMyMoveView(moveView);
				}
				//初始化学友列表
				mainActivity.item08View.init_Study_List();
				moveView.setMainView(mainActivity.item08View, null,8);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("账号设置")){
			if(!startInit(9)){
				//打开视图
				setCurrentTab(9);
				if(mainActivity.item09View.view==null){
					mainActivity.item09View.init();
					mainActivity.item09View.setMyMoveView(moveView);
				}
				moveView.setMainView(mainActivity.item09View, null,9);
			}
		}else if(childText[groupPosition][childPosition].toString().equals("下载管理")){
			if(!startInit(10)){
				//打开视图
				setCurrentTab(10);
				if(mainActivity.item10View.view==null){
					mainActivity.item10View.setMyMoveView(moveView);
				}
				mainActivity.item10View.init();
				moveView.setMainView(mainActivity.item10View, null,10);
			}
		}
	}
	
	
	public boolean startInit(int tabId){
		if(moveView.getNowState()==MyMoveView.MAIN){
			return true;
		}
		
		if(currentTab == tabId ){
			int now_state = moveView.getNowState();
			if (now_state == MyMoveView.MAIN) {
				moveView.moveToLeft(true);
			} else {
				moveView.moveToMain(true,0);
			}
			return true;
		}else{
			
			initLeftMenuBackGround();

			return false;
		}
	}
	
	/**
	 * 初始化左边菜单背景
	 */
	private void initLeftMenuBackGround(){
		this.mExpandableListView.setBackgroundResource(R.drawable.bj);
	}

	public void setWidth(int w) { 
		// 为了使侧边栏布局合理 所以要为某项布局中宽度fill_parent的控件设置绝对宽度即侧边栏的宽度 
		LayoutParams p = view.getLayoutParams();
		p.width = w;
		view.setLayoutParams(p);
		
		ViewGroup.LayoutParams params = titleLayout.getLayoutParams();
		params.width = w;
		titleLayout.setLayoutParams(params);
	}
	public void setCurrentTab(int currentTab) {
		this.currentTab = currentTab;
	}

	public int getCurrentTab() {
		return currentTab;
	}
	
	public View getView() {
		return view;
	}
	
	
	public class MyListAdapter extends BaseExpandableListAdapter{
		View mContext;
		public MyListAdapter(View leftMenuView) {
			mContext = leftMenuView;
		}

		@Override
		public String getChild(int groupPosition, int childPosition) {
			return childText[groupPosition][childPosition];
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				   boolean isLastChild, View convertView, ViewGroup parent) {
			GroupHolder groupHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext.getContext()).inflate(R.layout.leftmenu_list_item_enable, null);
				groupHolder=new GroupHolder();
				groupHolder.img = (ImageView) convertView.findViewById(R.id.left_menu_list_enable_img);
				groupHolder.title = (TextView) convertView.findViewById(R.id.left_menu_list_enable_text);
				groupHolder.msgNum=(TextView)convertView.findViewById(R.id.left_menu_list_msg_num);
				convertView.setTag(groupHolder);
			}else {
				groupHolder = (GroupHolder) convertView.getTag();
			}
			
			groupHolder.title.setText(childText[groupPosition][childPosition]);
			//mImgs
			groupHolder.img.setBackgroundResource(childImg[groupPosition][childPosition]);
			//Leone add显示推送消息总数
			msgNum = PushMessageReceiver.MSG_NOTICE+PushMessageReceiver.MSG_ME
					+PushMessageReceiver.MSG_COMMENT+PushMessageReceiver.MSG_FUNS
					+PushMessageReceiver.MSG_FRIEND+PushMessageReceiver.MSG_CHAT;
			
			if(childText[groupPosition][childPosition].toString().equals("消息") && msgNum!=0){
				groupHolder.msgNum.setVisibility(View.VISIBLE);
				groupHolder.msgNum.setText(""+msgNum);//msgNum
			}else{
				groupHolder.msgNum.setVisibility(View.GONE);
			}
			
			
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return childText[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		@Override
		public int getGroupCount() {
			return groups.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		class GroupHolder {
			ImageView img;
			TextView title;
			TextView msgNum;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				   View convertView, ViewGroup parent) {
			GroupHolder groupHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext.getContext()).inflate(R.layout.leftmenu_list_item, null);
				groupHolder = new GroupHolder();
				groupHolder.title = (TextView) convertView.findViewById(R.id.left_menu_list_text);
				convertView.setTag(groupHolder);
			} else {
				groupHolder = (GroupHolder) convertView.getTag();
			}
			groupHolder.title.setText(groups[groupPosition]);

			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}	
		
	}

	public void refreshView(){
		if(view!=null)
		view.invalidate();
	}	
}
